package cn.lystudio.rohmtimeopwide_background_v11.controller.appApi;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.util.ObjectUtil;
import cn.lystudio.rohmtimeopwide_background_v11.entity.po.UsmCdRecordPO;
import cn.lystudio.rohmtimeopwide_background_v11.service.UsmCdRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/app-api/usmcdrecord")
@CrossOrigin
@SaIgnore
@Slf4j
public class AppUsmCdRecordController {
    private UsmCdRecordService usmCdRecordService;
    @Autowired
    public void setUsmCdRecordService(UsmCdRecordService usmCdRecordService) {
        this.usmCdRecordService = usmCdRecordService;
    }

    @PostMapping("/addUsmCdRecord")
    public SaResult addUsmCdRecord(@RequestBody UsmCdRecordPO usmCdRecordPO){
        int res = usmCdRecordService.addRecord(usmCdRecordPO);
        if(res<1) return SaResult.error("添加失败");
        return SaResult.ok();
    }

    @GetMapping("/getUsmCdRecordUnfinished")
    public SaResult getUsmCdRecord(String machineNo,String lotNo){
        UsmCdRecordPO usmCdRecordPO = new UsmCdRecordPO();
        usmCdRecordPO.setMachineNo(machineNo);
        UsmCdRecordPO res = usmCdRecordService.getRecordUnfinished(usmCdRecordPO);
        if(ObjectUtil.isEmpty(res)) return SaResult.error("新记录");
        return SaResult.data(res);
    }

    @GetMapping("/getFinishedRecordByLotNo")
    public SaResult getFinishedRecordByLotNo(String lotNo){
        return SaResult.data(usmCdRecordService.getFinishedRecordByLotNo(lotNo));
    }

    @GetMapping("/getUsmCdRecordByID")
    public  SaResult getUsmCdRecordByID(String recordID){
        UsmCdRecordPO record = usmCdRecordService.getUsmCdRecordByID(recordID);
        if(ObjectUtil.isEmpty(record)) return SaResult.error("数据获取异常");
        return SaResult.data(record);
    }

    @PostMapping("/uptUsmCdRecord")
    public SaResult uptUsmCdRecord(@RequestBody UsmCdRecordPO usmCdRecordPO){
        int res = usmCdRecordService.uptRecord(usmCdRecordPO);
        if(res<1) return SaResult.error("更新失败");
        return SaResult.ok();
    }

    @PostMapping("/delUsmCdRecord")
    public SaResult delUsmCdRecord(@RequestBody UsmCdRecordPO usmCdRecordPO){
        int res = usmCdRecordService.delRecord(usmCdRecordPO.getRecordID());
        if(res<1) return SaResult.error("删除失败");
        return SaResult.ok();
    }

}
